
//
//  YJVisitorView.swift
//  YJWeiBo
//
//  Created by Jim1024 on 2017/1/13.
//  Copyright © 2017年 Jim1024. All rights reserved.
//

import UIKit

// 1.定义协议,继承和类的格式是一样的,不是OC的<NSObject>, 当协议方法定义为 optional,协议需要加上@objc
@objc protocol YJVisitorViewDelegate: NSObjectProtocol {
    
    @objc optional func visitorViewDidClickRegisterButton()
    
    @objc optional func visitorViewDidClickLoginButton()
}

class YJVisitorView: UIView {

    // 代理需要设置weak, 不然可能会循环引用
    weak var delegate: YJVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initCustomView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     设置访客视图
     
     - parameter imageName: 图片名称
     - parameter message:   文字
     */
    func setup(imageName: String, message: String) {
        homeView.isHidden = true
        sendSubview(toBack: coverView)
        
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
    }
    
    /// 开始旋转
    func startRotation() {
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        rotation.toValue = M_PI * 2
        rotation.duration = 20
        rotation.repeatCount = MAXFLOAT
        
        // 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        rotation.isRemovedOnCompletion = false
        
        iconView.layer.add(rotation, forKey: nil)
    }
    
    fileprivate func initCustomView() {
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 1.添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        /*================= layout =================*/
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-40)
        }
        
        coverView.contentMode = .scaleToFill
        coverView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        homeView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(16)
            make.width.equalTo(250)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.leading.equalTo(messageLabel)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.width.height.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
        }
        
    }
    
    // @objc 让让系统可以找到swift的方法, 系统会为swift方法自动添加 @objc,系统不会为private修饰的方法添加 @objc
    // MARK: - 按钮点击事件
    
    @objc fileprivate func didClickRegisterButton(_ button: UIButton) {
        // 4.调用代理的方法
        // visitorViewDidClickRegisterButton?: 表示代理实现了这个方法就调用这个方法,没有实现就不调用
        delegate?.visitorViewDidClickRegisterButton?()
    }
    
    @objc fileprivate func didClickLoginButton() {
        // Modal出控制器只有控制器才行
        // 4.调用代理的方法
        delegate?.visitorViewDidClickLoginButton?()
    }
    
    // MARK: - 懒加载
    
    /// 转轮
    fileprivate lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩
    fileprivate lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 小房子
    fileprivate lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 消息label
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,看看有什么惊喜!!"
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 18)
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState())
        //        button.titleLabel: 不要使用这个来设置按钮的label
        button.setTitle("注册", for: UIControlState())
        button.setTitleColor(UIColor.orange, for: UIControlState())
        
        // 添加点击事件
        button.addTarget(self, action: #selector(YJVisitorView.didClickRegisterButton(_:)), for: UIControlEvents.touchUpInside)
        
        return button
    }()
    
    /// 登录按钮
    fileprivate lazy var loginButton: UIButton = {
        var button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState())
        button.setTitle("登录", for: UIControlState())
        button.setTitleColor(UIColor.black, for: UIControlState())
        
        // 添加点击事件
        button.addTarget(self, action: #selector(YJVisitorView.didClickLoginButton), for: UIControlEvents.touchUpInside)
        
        return button
    }()

}
